package cluster

import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.cluster.client.ClusterClientReceptionist
import cluster.Cluster1.sparkWoker
import com.typesafe.config.ConfigFactory

/**
 * @Description: TODO
 * @Author: DJJ
 * @Date: 2019/12/13 10:19
 */
class Cluster2 extends Actor {

  override def receive: Receive = {
    case "start" => println("Cluster2 start")
    case "master" =>
      println("---Cluster2收到------")
      println(sender())

    case ClusterMessage(path) =>
      println("收到集群客户端的消息----Cluster2")
      val unit = context.actorSelection(path)
      unit ! "hello"
  }

}

object Cluster2 extends App {
  val config = ConfigFactory.parseString(
    s"""
       |akka.remote.netty.tcp.hostname="127.0.0.1"
       |akka.remote.netty.tcp.port=9991
        """.stripMargin).withFallback(ConfigFactory.load())
  val sparkWoker = ActorSystem("Cluster", config)
  val requestLimiter: ActorRef = {
    sparkWoker.actorOf(Props[Cluster2], "Cluster")
  }
  /**
   * 注册,对外提供集群服务
   */
  ClusterClientReceptionist(sparkWoker).registerService(requestLimiter)
}
